//
//  XVCProductSeachResultsShow.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/21.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVCProductSeachResultsShow.h"
#import "XXTool.h"
#import "ProductInformationShowCell.h"
#import "XVCProductDetailShow.h"

@interface XVCProductSeachResultsShow ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableview;

@end

@implementation XVCProductSeachResultsShow
{
    NSMutableArray <XMLProductInformationShowModel *> *useData;
}
-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = [XXTool colorWithBackGround];
        MJRefreshNormalHeader *useHead = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        [useHead setTitle:NSLocalizedString(@"下拉刷新", nil) forState:MJRefreshStateIdle];
        [useHead setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useHead setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_header = useHead;
        
        MJRefreshBackNormalFooter *useFooter = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReFeren)];
        [useFooter setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useFooter setTitle:NSLocalizedString(@"上拉刷新", nil) forState:MJRefreshStateIdle];
        [useFooter setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_footer = useFooter;
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

-(void)initParameter
{
    
}

-(void)initUI
{
    [self.navigationItem setTitle:NSLocalizedString(@"搜索结果", nil)];
    [self tableview];
}

-(void)initData
{
    [self.view startLoading];
    [self getData];
}

-(void)refreshLayout:(CGRect)frame
{
    _tableview.frame = CGRectMake(0, 0, frame.size.width
                                  , frame.size.height);
}

#pragma mark tableviewdatasouce
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProductInformationShowCell * cell = [ProductInformationShowCell cellSelectionStyleNoneWithTableView:tableView];
    [cell setCellWithData:useData[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return useData.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark tableviewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMLProductInformationShowModel *temp = useData[indexPath.row];
    NSString *exName = @"";
    if ([XXTool didCurrentLanguageChinese])
    {
        exName = temp.cpname;
    }
    else
    {
        exName = temp.cpname_en;
    }
    [XVCProductDetailShow
                        pushProductDetailVCWithSourceVC:self
                                               goods_id:temp.goods_id exhibitorName:exName];
}

-(void)getData
{
    [XMLProductInformationShowModelArray
     requestProuductSearchResultsListWithSearchKey:_searchKey
                                        searchType:_searchType
                                             Count:0
                                        completion:^(BOOL isSuccess, NSMutableArray<XMLProductInformationShowModel *> *datalist) {
                                            if (isSuccess)
                                            {
                                                if (datalist.count==0)
                                                {
                                                    [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"暂无相关搜索", nil)];
                                                }
                                                
                                                useData = datalist;
                                                [_tableview reloadData];
                                                [_tableview.mj_header endRefreshing];
                                                [self.view stopLoading];
                                            }
     }];
}

-(void)footerReFeren
{
    [XMLProductInformationShowModelArray
     requestProuductSearchResultsListWithSearchKey:_searchKey
                                        searchType:_searchType
                                             Count:useData.count
                                        completion:^(BOOL isSuccess, NSMutableArray<XMLProductInformationShowModel *> *datalist) {
                                         if (isSuccess)
                                         {
                                             if (datalist.count==0)
                                             {
                                                 [SVProgressHUD showInfoWithStatus:NSLocalizedString(@"暂无更多展品", nil)];
                                             }
                                             
                                             [useData addObjectsFromArray:datalist];
                                             [_tableview reloadData];
                                             [_tableview.mj_footer endRefreshing];
                                         }
                                     }];
}

#pragma mark api
+(void)pushProductSeachResultsShowVCWithSourceVC:(UIViewController *)sourceVC searchKey:(NSString *)key searchType:(NSString *)type
{
    XVCProductSeachResultsShow *vc = [[XVCProductSeachResultsShow alloc] init];
    vc.searchKey = key;
    vc.searchType = type;
    if (sourceVC)
    {
        [sourceVC.navigationController pushViewController:vc animated:YES];
    }
}

@end
